package com.fingard.avatar.website.utils;

import com.fingard.avatar.website.config.UserContext;
import com.fingard.avatar.website.model.enums.DeletedEnum;
import com.fingard.avatar.website.model.po.BasePo;

import java.time.LocalDateTime;

/**
 * @author 徐一涛
 * @date 2022/4/26
 */
public class DaoUtils {
    public static <T extends BasePo> void insert(T obj) {
        insert(obj, UserContext.getUserId());
    }
    public static <T extends BasePo> void insert(T obj, Long userId) {
        obj.setCreateBy(userId);
        obj.setCreateTime(LocalDateTime.now());
        obj.setUpdateBy(userId);
        obj.setUpdateTime(LocalDateTime.now());
        obj.setIsDeleted(DeletedEnum.NOT_DELETED.getValue());
        obj.setVersion(1);
    }
    public static <T extends BasePo> void update(T obj) {
        update(obj, UserContext.getUserId());
    }

    public static <T extends BasePo> void update(T obj, Long userId) {
        obj.setUpdateBy(userId);
        obj.setUpdateTime(LocalDateTime.now());
    }

    public static <T extends BasePo> void delete(T obj) {
        delete(obj, UserContext.getUserId());
    }

    public static <T extends BasePo> void delete(T obj, Long userId) {
        obj.setIsDeleted(DeletedEnum.DELETED.getValue());
        obj.setUpdateBy(userId);
        obj.setUpdateTime(LocalDateTime.now());
    }
}
